<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:cart:add']"
        >添加菜品</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cartList">
      <el-table-column label="桌id" align="center" prop="table_id" />
      <el-table-column label="菜品列表" align="center" prop="all_dish_names" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="danger" plain size="mini" @click="deleteRow(scope.row.table_id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加购物车对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="桌id" prop="tableId">
          <el-input v-model="form.tableId" placeholder="请输入桌id" />
        </el-form-item>
        <el-form-item label="菜id" prop="dishId">
          <el-input v-model="form.dishId" placeholder="请输入菜id" />
        </el-form-item>
        <el-form-item label="菜名" prop="dishName">
          <el-input v-model="form.dishName" placeholder="请输入菜名" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="添加时间" prop="addTime">
          <el-date-picker clearable
                          v-model="form.addTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择添加时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listCart,
  addCart,
  deleteCartByTableId
} from '@/api/project/cart'

export default {
  name: "Cart",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物车表格数据
      cartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 添加时间时间范围
      daterangeAddTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cartId: null,
        tableId: null,
        dishId: null,
        dishName: null,
        price: null,
        quantity: null,
        addTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      partialDeleteDialogVisible: false,
      itemsToDelete: [],
      selectedRow: null,
      deleteConfirmationDialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    deleteRow(tableId) {
      // 这里可以根据 tableId 调用后端接口进行删除操作
      // 假设删除接口函数为 deleteCartByTableId
      deleteCartByTableId(tableId).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(error => {
        this.$modal.msgError("删除失败");
      });
    },
    getList() {
      this.loading = true;
      listCart(this.queryParams).then((response) => {
        this.cartList = response.list.map(item => ({
          table_id: item.table_id,
          all_dish_names: item.all_dish_names,
        }));
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cartId: null,
        tableId: null,
        dishId: null,
        dishName: null,
        price: null,
        quantity: null,
        addTime: null,
        cartList: null,
      };
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cartId!= null) {
            updateCart(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCart(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购物车";
    },
  },
};
</script>
